package framework;

import java.io.File;
import org.newdawn.slick.Animation;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;

public class NartanAnimationManager implements AnimationManager {

	protected SpriteSheet sprites;
	
	protected String heroName;

	protected Image sprite;

	protected Animation selectAnimation;
	protected Animation selectAnimationBackward;
	
	protected Animation walkingAnimation;
	protected Animation walkingAnimationBackward;

	protected Animation idleAnimation;
	protected Animation idleAnimationBackward;

	protected Animation jumpingAnimation;
	protected Animation jumpingAnimationBackward;

	protected Animation guardAnimation;
	protected Animation guardAnimationBackward;
	
	protected Animation hitAnimation;
	protected Animation hitAnimationBackward;
	
	protected Animation dyingAnimation;
	protected Animation dyingAnimationBackward;
	
	protected Image mini;
	protected Image miniBackward;

	protected static final int ANIMATION_DELAY = 400;

	public NartanAnimationManager() {
		this("");
	}

	public NartanAnimationManager(String heroName) {
		this.heroName = heroName;
		try {
			this.sprite = new Image(C.ASSETS_CHAR_FOLDER + "Nartan" + File.separator + "sprites.png");
		} catch (SlickException e) {
			e.printStackTrace();
		}
		sprites = new SpriteSheet(this.sprite, 384, 192);
		sprites.flushPixelData();
		init();
	}
	
	public NartanAnimationManager(SpriteSheet sprites) {
		this.sprites = sprites;
		init();
	}
	
	public void init() {
		initIdleAnimation();
		initJumpingAnimation();
		initWalkingAnimation();
		initGuardAnimation();
		initHitAnimation();
		initDyingAnimation();
		initMini();
		initSelectAnimation();
	}
	
	@Override
	public void initDyingAnimation() {
		Image[] frames = new Image[2];
		frames[0] = getSpriteSheet().getSprite(3, 1);
		frames[1] = getSpriteSheet().getSprite(4, 1);
		this.dyingAnimation = new Animation(frames, 300);
		this.dyingAnimation.setDuration(1, 2000);
		this.dyingAnimation.setLooping(false);
		this.dyingAnimationBackward = Util.getFlippedAnimation(this.dyingAnimation, true, false);
		this.dyingAnimationBackward.setLooping(false);
	}

	@Override
	public void initSelectAnimation() {
		Image[] frames = new Image[2];
		frames[0] = getSpriteSheet().getSprite(0, 2);
		frames[1] = getSpriteSheet().getSprite(1, 2);
		this.selectAnimation = new Animation(frames, 150);
		this.selectAnimation.setDuration(1, 300);
		this.selectAnimation.setLooping(false);
		this.selectAnimationBackward = Util.getFlippedAnimation(this.selectAnimation, true, false);
		this.selectAnimationBackward.setLooping(false);
	}
	
	@Override
	public void initWalkingAnimation() {
		Image[] frames = new Image[4];
		frames[0] = getSpriteSheet().getSprite(0, 0);
		frames[1] = getSpriteSheet().getSprite(1, 0);
		frames[2] = getSpriteSheet().getSprite(2, 0);
		frames[3] = getSpriteSheet().getSprite(3, 0);
		this.walkingAnimation = new Animation(frames, 100);
		this.walkingAnimationBackward = Util.getFlippedAnimation(this.walkingAnimation, true, false);
	}

	@Override
	public void initIdleAnimation() {
		Image[] frames = new Image[2];
		frames[0] = getSpriteSheet().getSprite(0, 0);
		frames[1] = getSpriteSheet().getSprite(4, 0);
		this.idleAnimation = new Animation(frames, 300);
		this.idleAnimationBackward = Util.getFlippedAnimation(this.idleAnimation, true, false);
	}

	@Override
	public void initJumpingAnimation() {
		Image[] frames = new Image[2];
		frames[0] = getSpriteSheet().getSprite(0, 1);
		frames[1] = getSpriteSheet().getSprite(1, 1);
		this.jumpingAnimation = new Animation(frames, 6000);
		this.jumpingAnimationBackward =  Util.getFlippedAnimation(this.jumpingAnimation, true, false);
	}

	@Override
	public void initGuardAnimation() {
		Image[] frames = new Image[1];
		frames[0] = getSpriteSheet().getSprite(2, 1);
		this.guardAnimation = new Animation(frames, 300);
		this.guardAnimationBackward = Util.getFlippedAnimation(this.guardAnimation, true, false);
	}	
	
	@Override
	public void initHitAnimation() {
		Image[] frames = new Image[2];
		frames[0] = getSpriteSheet().getSprite(3, 1);
		frames[1] = frames[0].copy();
		frames[1].setAlpha(0.3f);
		this.hitAnimation = new Animation(frames, 50);
		this.hitAnimationBackward =  Util.getFlippedAnimation(this.hitAnimation, true, false);
	}
	
	@Override
	public void initMini() {
		Image sub = getSpriteSheet().getSprite(0, 0);	
		sub = sub.getSubImage(125, 10, 120, 120);
		this.mini = sub;
		this.miniBackward = sub.getFlippedCopy(true, false);
	}

	@Override
	public void setSpriteSheet(SpriteSheet sprites) {
		this.sprites = sprites;
	}

	@Override
	public SpriteSheet getSpriteSheet() {
		return this.sprites;
	}

	@Override
	public Animation getWalkingAnimation(boolean forward) {
		return forward ? this.walkingAnimation : this.walkingAnimationBackward;
	}

	@Override
	public Animation getJumpingAnimation(boolean forward) {
		return forward ? this.jumpingAnimation : this.jumpingAnimationBackward;
	}

	@Override
	public Animation getIdleAnimation(boolean forward) {
		return forward ? this.idleAnimation : this.idleAnimationBackward;
	}

	@Override
	public Animation getGuardAnimation(boolean forward) {
		return forward ? this.guardAnimation : this.guardAnimationBackward;
	}
	
	@Override
	public Animation getHitAnimation(boolean forward) {
		return forward ? this.hitAnimation : this.hitAnimationBackward;
	}

	@Override
	public Animation getDyingAnimation(boolean forward) {
		return forward ? this.dyingAnimation : this.dyingAnimationBackward;
	}
	
	@Override
	public Animation getSelectAnimation(boolean forward) {
		return forward ? this.selectAnimation : this.selectAnimationBackward;
	}

	@Override
	public Image getMini(boolean forward) {
		return forward ? this.mini : this.miniBackward;
	}
}
